

from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU

# 订单简单序列化 —— 作用于序列化返回订单列表数据接口
class OrderSimpleModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time'
        ]


# 定义关联的SKU模型类序列化器
class SKUSimpleModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image'
        ]


# 定义关联的OrderGoods模型类序列化器
class OrderGoodsModelSerializer(serializers.ModelSerializer):
    # 关联的主表SKU单一对象
    sku = SKUSimpleModelSerializer()

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku',
        ]


# 订单详情序列化 —— 作用于序列化返回单一订单详细数据接口
class OrderDetailModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()
    # 自定义序列化OrderGoods列表数据
    skus = OrderGoodsModelSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = [
            # 固有字段
            'order_id',
            'total_count',
            'total_amount',
            'freight',
            'pay_method',
            'status',
            'create_time',

            # 主表关联字段(显示定义)
            'user',

            # 从表关联字段(隐藏定义)
            # 关联的从表 —— 订单商品(OrderGoods)中间表多条数据
            'skus',
        ]











